//
//  Int+Extension.swift
//  Lease_IOS
//
//  Created by Mr.wang on 2018/12/20.
//  Copyright © 2018 Mr.wang. All rights reserved.
//     __            __            __
//     \ \          /  \          / /     帅™
//      \ \        / /\ \        / /
//       \ \      / /  \ \      / /
//        \ \    / /    \ \    / /
//         \ \  / /      \ \  / /
//          \ \/ /        \ \/ /
//           \__/          \__/


import RxSwift

public extension Int {
    
    func countdown(_ closure: @escaping (Int) -> String = { "\($0)秒" }, resend: String = "重新发送")
        -> Observable<(title: String, isEnabled: Bool)> {
            
            return Observable.of(self)
                .flatMap { until -> Observable<(title: String, isEnabled: Bool)> in
                    Observable<Int>
                        .timer(.seconds(0), period: .seconds(1), scheduler: ConcurrentDispatchQueueScheduler.init(qos: .background))
                        .map {
                            if until > $0 {
                                return (closure(until - $0), false)
                            }
                            throw NullError()
                        }.catchErrorJustReturn((resend, true))
            }
            
    }
    
}
